<template>
  <div class="app-container">
    <el-dialog
      title="考勤状态"
      :visible.sync="dialog.isShow"
    >
      <el-radio-group v-model="dialog.status">
        <el-radio v-for="(item,index) in dialog.statusList" :key="index" :label="item.message" border/>
      </el-radio-group>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.isShow = false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
        
      </el-form-item>
  

        <el-form-item label="月份" prop="month">
          <el-date-picker
          format="yyyy/MM"
          value-format='yyyyMM'
            type="month"
            placeholder='请选择月份'
            v-model="queryParams.month"
            @change="handleQuery"
          />
        </el-form-item>


        
 
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hrmanagement:attendance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hrmanagement:attendance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hrmanagement:attendance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hrmanagement:attendance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="attendanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="员工号" align="center" prop="userId" /> -->
      <el-table-column label="员工姓名" align="center" prop="name" width="180"/>
      <el-table-column label="手机号" align="center" prop="phone" width="180"/>
      <el-table-column label="部门名" align="center" prop="deptName" width="180"/>
      <el-table-column v-for="index in dayNums" :label="index+'日'" :key="index" min-width="55">
          <template slot-scope="scope">
            <div @click="changeStatus(scope.row,index-1)">
              <el-tag :type="scope.row.kqList[index-1].tagType">
                {{ scope.row.kqList[index - 1].message }}
              </el-tag>
            </div>
          </template>
        </el-table-column>


      <!-- <el-table-column label="考勤日期" align="center" prop="attendanceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.attendanceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="员工姓名" align="center" prop="staffName" /> -->
      

     
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hrmanagement:attendance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hrmanagement:attendance:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工考勤对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工id" prop="staffId">
          <el-input v-model="form.staffId" placeholder="请输入员工id" />
        </el-form-item>
        <el-form-item label="上午上班时间" prop="morStartTime">
          <el-date-picker clearable
            v-model="form.morStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上午上班时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上午下班时间" prop="morEndTime">
          <el-date-picker clearable
            v-model="form.morEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上午下班时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下午上班时间" prop="aftStartTime">
          <el-date-picker clearable
            v-model="form.aftStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下午上班时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下午下班时间" prop="aftEndTime">
          <el-date-picker clearable
            v-model="form.aftEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下午下班时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="考勤日期" prop="attendanceDate">
          <el-date-picker clearable
            v-model="form.attendanceDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择考勤日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="员工姓名" prop="staffName">
          <el-input v-model="form.staffName" placeholder="请输入员工姓名" />

   
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getAll, setAttendance,getByStaffIdAndDate,listAttendance, getAttendance, delAttendance, addAttendance, updateAttendance } from "@/api/hrmanagement/attendance";

export default {
  name: "Attendance",
  data() {
    return {
      
      dayNums:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工考勤表格数据
      attendanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffName: null,
       
      },

      formData: {},

      dialog: {
        isShow: false,
        data: {},
        status: '',
        statusList: []
      },


      // 表单参数
      form: {},
      // 表单校验
      rules: {
        attendanceDate: [
          { required: true, message: "考勤日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    
    this.getList();
  },
 
  methods: {
    confirm () {
      console.log("sdffffffffff");
      console.log(this.dialog.data);
      console.log( this.dialog.status);
      this.dialog.data.status = this.dialog.status
      setAttendance(this.dialog.data).then(response => {
        if (response.code === 200) {
       
          console.log("sdfffffffffffffffffffffffffff");
          this.$message.success('修改成功')
          
          this.dialog.isShow = false
        } else {
          this.$message.error('修改失败')
        }
        this.getList();
      })
    },
    changeStatus (row, i) {
      console.log();
      getAll().then(response => {
        if (response.code === 200) {
          this.dialog.statusList = response.data
        } else {
          this.$message.error('获取数据失败')
        }
      })
    

      getByStaffIdAndDate(row.userId, row.kqList[i].attendanceDate).then(response => {
        if (response.msg === "操作成功") {
          this.dialog.data = response.data
        } else {
          this.dialog.data = {
            staffId: row.userId,
            attendanceDate: row.kqList[i].attendanceDate
          }
    
        }
      })
      this.dialog.isShow = true
      this.dialog.status = row.kqList[i].message
    },
    /** 查询员工考勤列表 */
    getList() {
      this.loading = true;
      listAttendance(this.queryParams,this.formData.month).then(response => {
        this.dayNums = response.num
        this.attendanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        staffId: null,
        morStartTime: null,
        morEndTime: null,
        aftStartTime: null,
        aftEndTime: null,
        attendanceDate: null,
        status: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        staffName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工考勤";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAttendance(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工考勤";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAttendance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttendance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工考勤编号为"' + ids + '"的数据项？').then(function() {
        return delAttendance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hrmanagement/attendance/export', {
        ...this.queryParams
      }, `attendance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
